package com.material.supplier.service.impl;

import com.material.common.core.domain.entity.SysUser;
import com.material.common.utils.bean.BeanUtil;
import com.material.supplier.domain.SupplierAccount;
import com.material.supplier.mapper.SupplierAccountMapper;
import com.material.supplier.service.ISupplierAccountService;
import com.material.supplier.vo.SupplierAccountVo;
import com.material.system.mapper.SysUserMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ganxinge
 */
@Service
public class SupplierAccountServiceImpl implements ISupplierAccountService {

    @Autowired
    private SupplierAccountMapper supplierAccountMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public SupplierAccount selectSupplierAccountById(Long id) {
        return supplierAccountMapper.selectSupplierAccountById(id);
    }

    @Override
    public List<SupplierAccount> selectSupplierAccountList(SupplierAccount supplierAccount) {
        return supplierAccountMapper.selectSupplierAccountList(supplierAccount);
    }

    @Override
    public List<SupplierAccountVo> selectSupplierAccountVoList(SupplierAccount supplierAccount) {
        List<SupplierAccount> supplierAccounts = supplierAccountMapper.selectSupplierAccountList(supplierAccount);
        if(CollectionUtils.isEmpty(supplierAccounts)){
            return new ArrayList<>();
        }
        List<Long> supplierIds = supplierAccounts.stream().map(SupplierAccount::getId).collect(Collectors.toList());
        List<SysUser> sysUsers = sysUserMapper.selectSupplierList(supplierIds);
        Map<Long, String> supplierMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getBizId, s -> s.getAvatar()));
        List<SupplierAccountVo> supplierAccountVos = BeanUtil.copyProperties(supplierAccounts, SupplierAccountVo.class);
        supplierAccountVos.forEach(s->{
            s.setAvatar(supplierMap.get(s.getId()));
        });
        return supplierAccountVos;
    }

    @Override
    public int insertSupplierAccount(SupplierAccount supplierAccount) {
        return supplierAccountMapper.insertSupplierAccount(supplierAccount);
    }

    @Override
    public int updateSupplierAccount(SupplierAccount supplierAccount) {
        return supplierAccountMapper.updateSupplierAccount(supplierAccount);
    }

    @Override
    public SupplierAccount selectSupplierAccountBySupplierId(Long supplierId) {
        return supplierAccountMapper.selectSupplierAccountBySupplierId(supplierId);
    }

    @Override
    public SupplierAccount selectSupplierAccountByLoginName(String loginName) {
        return supplierAccountMapper.selectSupplierAccountByLoginName(loginName);
    }

    @Override
    public int batchInsertSupplierAccount(List<SupplierAccount> addAccount) {
        return supplierAccountMapper.batchInsertSupplierAccount(addAccount);
    }
}
